###This produces the tabular results for candidate stereotyping of traits in Replication Data for: Disputed Ownership: Parties, Issues, and Traits in the Minds of Voters by Stephen N. Goggin and Alexander G. Theodoridis

##Need to set this:
setwd("~/Desktop/DisputedOwnership_Replication")

library(foreign)
library(car)
library(boot)
library(bootstrap)

cces <- read.csv("trait_stereotyping.csv")
attach(cces)

#Note these levels of candidate and respondent party
#candparty -> 1 = Dem, 2 = Rep, 3 = None
#pid3lean -> -1 = Dem, 0 = Ind, 1 = Rep
candparty <- AGT303_treatt

##Creating Trait Variables
Comp <- as.numeric(AGT303alt_a)
Mora <- as.numeric(AGT303alt_b)
Lead <- as.numeric(AGT303alt_c)
Empa <- as.numeric(AGT303alt_d)
Know <- as.numeric(AGT303alt_e)
Gree <- as.numeric(AGT303alt_f)
Inde <- as.numeric(AGT303alt_g)
Hard <- as.numeric(AGT303alt_h)
Hone <- as.numeric(AGT303alt_i)

Comp[Comp==8 | Comp==9] <- NA
Mora[Mora==8 | Mora==9] <- NA
Lead[Lead==8 | Lead==9] <- NA
Empa[Empa==8 | Empa==9] <- NA
Know[Know==8 | Know==9] <- NA
Gree[Gree==8 | Gree==9] <- NA
Inde[Inde==8 | Inde==9] <- NA
Hard[Hard==8 | Hard==9] <- NA
Hone[Hone==8 | Hone==9] <- NA

Comp <- Comp + -4
Mora <- Mora + -4
Lead <- Lead + -4
Empa <- Empa + -4
Know <- Know + -4
Gree <- Gree + -4
Inde <- Inde + -4
Hard <- Hard + -4
Hone <- Hone + -4

####################
#Create Data Frame with Essential Variables
cces_ready <- data.frame(V101,candparty,pid3lean,Comp,Mora,Lead,Empa,Know,Hard,Hone)

#Creating Vectors for Output
MDiff_Comp <- NULL
MDiff_Mora <- NULL
MDiff_Lead <- NULL
MDiff_Empa <- NULL
MDiff_Know <- NULL
MDiff_Hard <- NULL
MDiff_Hone <- NULL

####################
#Bootstrapping it all

for (i in 1:10000){
	
tempdata <- sample(1:nrow(cces_ready),nrow(cces_ready),replace=T)

partisans <- cces_ready[tempdata,]

D_D <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==1)
D_R <- subset(partisans, partisans$pid3lean==-1 & partisans$candparty==2)
R_R <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==2)
R_D <- subset(partisans, partisans$pid3lean==1 & partisans$candparty==1)


D_D_Comp <- mean(D_D$Comp,na.rm=T)
D_R_Comp <- mean(D_R$Comp,na.rm=T)
R_R_Comp <- mean(R_R$Comp,na.rm=T)
R_D_Comp <- mean(R_D$Comp,na.rm=T)

D_D_Mora <- mean(D_D$Mora,na.rm=T)
D_R_Mora <- mean(D_R$Mora,na.rm=T)
R_R_Mora <- mean(R_R$Mora,na.rm=T)
R_D_Mora <- mean(R_D$Mora,na.rm=T)

D_D_Lead <- mean(D_D$Lead,na.rm=T)
D_R_Lead <- mean(D_R$Lead,na.rm=T)
R_R_Lead <- mean(R_R$Lead,na.rm=T)
R_D_Lead <- mean(R_D$Lead,na.rm=T)

D_D_Empa <- mean(D_D$Empa,na.rm=T)
D_R_Empa <- mean(D_R$Empa,na.rm=T)
R_R_Empa <- mean(R_R$Empa,na.rm=T)
R_D_Empa <- mean(R_D$Empa,na.rm=T)

D_D_Know <- mean(D_D$Know,na.rm=T)
D_R_Know <- mean(D_R$Know,na.rm=T)
R_R_Know <- mean(R_R$Know,na.rm=T)
R_D_Know <- mean(R_D$Know,na.rm=T)

D_D_Hard <- mean(D_D$Hard,na.rm=T)
D_R_Hard <- mean(D_R$Hard,na.rm=T)
R_R_Hard <- mean(R_R$Hard,na.rm=T)
R_D_Hard <- mean(R_D$Hard,na.rm=T)

D_D_Hone <- mean(D_D$Hone,na.rm=T)
D_R_Hone <- mean(D_R$Hone,na.rm=T)
R_R_Hone <- mean(R_R$Hone,na.rm=T)
R_D_Hone <- mean(R_D$Hone,na.rm=T)
	
D_Comp <- D_D_Comp - D_R_Comp
R_Comp <- R_R_Comp - R_D_Comp
D_Mora <- D_D_Mora - D_R_Mora
R_Mora <- R_R_Mora - R_D_Mora
D_Lead <- D_D_Lead - D_R_Lead
R_Lead <- R_R_Lead - R_D_Lead
D_Empa <- D_D_Empa - D_R_Empa
R_Empa <- R_R_Empa - R_D_Empa
D_Know <- D_D_Know - D_R_Know
R_Know <- R_R_Know - R_D_Know
D_Hard <- D_D_Hard - D_R_Hard
R_Hard <- R_R_Hard - R_D_Hard
D_Hone <- D_D_Hone - D_R_Hone
R_Hone <- R_R_Hone - R_D_Hone

Diff_Comp <- D_Comp - R_Comp
Diff_Mora <- D_Mora - R_Mora
Diff_Lead <- D_Lead - R_Lead
Diff_Empa <- D_Empa - R_Empa
Diff_Know <- D_Know - R_Know
Diff_Hard <- D_Hard - R_Hard
Diff_Hone <- D_Hone - R_Hone

Diffs <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone)

Mean_Diff <- mean(Diffs)

MDiff_Comp <- c(MDiff_Comp, Diff_Comp - Mean_Diff)
MDiff_Mora <- c(MDiff_Mora, Diff_Mora - Mean_Diff)
MDiff_Lead <- c(MDiff_Lead, Diff_Lead - Mean_Diff)
MDiff_Empa <- c(MDiff_Empa, Diff_Empa - Mean_Diff)
MDiff_Know <- c(MDiff_Know, Diff_Know - Mean_Diff)
MDiff_Hard <- c(MDiff_Hard, Diff_Hard - Mean_Diff)
MDiff_Hone <- c(MDiff_Hone, Diff_Hone - Mean_Diff)
		
}

####################
#Calculating the Actual Differences in Sample

D_D <- subset(cces_ready, cces_ready$pid3lean==-1 & cces_ready$candparty==1)
D_R <- subset(cces_ready, cces_ready$pid3lean==-1 & cces_ready$candparty==2)
R_R <- subset(cces_ready, cces_ready$pid3lean==1 & cces_ready$candparty==2)
R_D <- subset(cces_ready, cces_ready$pid3lean==1 & cces_ready$candparty==1)

D_D_Comp <- mean(D_D$Comp,na.rm=T)
D_R_Comp <- mean(D_R$Comp,na.rm=T)
R_R_Comp <- mean(R_R$Comp,na.rm=T)
R_D_Comp <- mean(R_D$Comp,na.rm=T)

D_D_Mora <- mean(D_D$Mora,na.rm=T)
D_R_Mora <- mean(D_R$Mora,na.rm=T)
R_R_Mora <- mean(R_R$Mora,na.rm=T)
R_D_Mora <- mean(R_D$Mora,na.rm=T)

D_D_Lead <- mean(D_D$Lead,na.rm=T)
D_R_Lead <- mean(D_R$Lead,na.rm=T)
R_R_Lead <- mean(R_R$Lead,na.rm=T)
R_D_Lead <- mean(R_D$Lead,na.rm=T)

D_D_Empa <- mean(D_D$Empa,na.rm=T)
D_R_Empa <- mean(D_R$Empa,na.rm=T)
R_R_Empa <- mean(R_R$Empa,na.rm=T)
R_D_Empa <- mean(R_D$Empa,na.rm=T)

D_D_Know <- mean(D_D$Know,na.rm=T)
D_R_Know <- mean(D_R$Know,na.rm=T)
R_R_Know <- mean(R_R$Know,na.rm=T)
R_D_Know <- mean(R_D$Know,na.rm=T)

D_D_Hard <- mean(D_D$Hard,na.rm=T)
D_R_Hard <- mean(D_R$Hard,na.rm=T)
R_R_Hard <- mean(R_R$Hard,na.rm=T)
R_D_Hard <- mean(R_D$Hard,na.rm=T)

D_D_Hone <- mean(D_D$Hone,na.rm=T)
D_R_Hone <- mean(D_R$Hone,na.rm=T)
R_R_Hone <- mean(R_R$Hone,na.rm=T)
R_D_Hone <- mean(R_D$Hone,na.rm=T)
	
D_Comp <- D_D_Comp - D_R_Comp
R_Comp <- R_R_Comp - R_D_Comp
D_Mora <- D_D_Mora - D_R_Mora
R_Mora <- R_R_Mora - R_D_Mora
D_Lead <- D_D_Lead - D_R_Lead
R_Lead <- R_R_Lead - R_D_Lead
D_Empa <- D_D_Empa - D_R_Empa
R_Empa <- R_R_Empa - R_D_Empa
D_Know <- D_D_Know - D_R_Know
R_Know <- R_R_Know - R_D_Know
D_Hard <- D_D_Hard - D_R_Hard
R_Hard <- R_R_Hard - R_D_Hard
D_Hone <- D_D_Hone - D_R_Hone
R_Hone <- R_R_Hone - R_D_Hone

Diff_Comp <- D_Comp - R_Comp
Diff_Mora <- D_Mora - R_Mora
Diff_Lead <- D_Lead - R_Lead
Diff_Empa <- D_Empa - R_Empa
Diff_Know <- D_Know - R_Know
Diff_Hard <- D_Hard - R_Hard
Diff_Hone <- D_Hone - R_Hone

Diffs <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone)

Mean_Diff <- mean(Diffs)

#The actual differences of interest
ADiff_Comp <- Diff_Comp - Mean_Diff
ADiff_Mora <- Diff_Mora - Mean_Diff
ADiff_Lead <- Diff_Lead - Mean_Diff
ADiff_Empa <- Diff_Empa - Mean_Diff
ADiff_Know <- Diff_Know - Mean_Diff
ADiff_Hard <- Diff_Hard - Mean_Diff
ADiff_Hone <- Diff_Hone - Mean_Diff

####################
#Comparing the Actual Differences and Bootstrapped Bounds

trait <- c("Compassionate","Moral","Strong Leader","Really Cares","Knowledgeable","Hard-Working","Honest")

CI95_lower <- c(quantile(MDiff_Comp,.025),quantile(MDiff_Mora,.025)
,quantile(MDiff_Lead,.025),quantile(MDiff_Empa,.025),quantile(MDiff_Know,.025),quantile(MDiff_Hard,.025),quantile(MDiff_Hone,.025))

CI95_upper <- c(quantile(MDiff_Comp,.975),quantile(MDiff_Mora,.975)
,quantile(MDiff_Lead,.975),quantile(MDiff_Empa,.975),quantile(MDiff_Know,.975),quantile(MDiff_Hard,.975),quantile(MDiff_Hone,.975))

ownership_estimate <- c(ADiff_Comp,ADiff_Mora,ADiff_Lead,ADiff_Empa,ADiff_Know,ADiff_Hard,ADiff_Hone)

D_ratingdiff <- c(D_Comp, D_Mora, D_Lead, D_Empa, D_Know, D_Hard, D_Hone)

R_ratingdiff <- c(R_Comp, R_Mora, R_Lead, R_Empa, R_Know, R_Hard, R_Hone)

raw_Diff <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone)

ownership <- data.frame(trait,ownership_estimate, CI95_lower,CI95_upper,raw_Diff,D_ratingdiff,R_ratingdiff)

write.csv(ownership,"traitstereotyping.csv")


